using System;
using System.Collections.Generic;
using System.Text;

namespace WMOSSExperts.PackageBuilder
{
    /// <summary>
    /// A feature item in the SharePoint solution
    /// </summary>
    public class FeatureItem : BaseSolutionItem
    {

        /// <summary>
        /// The feature name
        /// </summary>
        private String _featureName;
        /// <summary>
        /// Gets the feature name from the folder structure
        /// </summary>
        public String FeatureName
        {
            get
            {
                if (String.IsNullOrEmpty(_featureName))
                {
                    Int32 pos = this.SourcePath.IndexOf(@"\12\Template\Features\", StringComparison.InvariantCultureIgnoreCase);
                    if (pos < 0) throw new ArgumentOutOfRangeException("element", @"the full path doesn't contain \12\Template\Features\");
                    String s = this.SourcePath.Substring(pos + 22);

                    pos = s.IndexOf(@"\");
                    if (pos >= 0)
                        s = s.Substring(0, pos);

                    _featureName = s;
                }
                return _featureName;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private Boolean _isManifest = false;

        public Boolean IsManifest
        {
            get
            {
                return _isManifest;
            }
            set { _isManifest = value; }
        }

        private Boolean _isManifestElement = false;
        public Boolean IsManifestElement
        {
            get
            {
                return _isManifestElement;
            }
            set { _isManifestElement = value; }
        }

        public override string Location
        {
            get
            {
                if (this.IsManifest | this.IsManifestElement)
                {
                    return String.Format(@"{0}\{1}", this.FeatureName, this.FileName);
                }
                else
                    return base.Location;
            }
            set
            {
                base.Location = value;
            }
        }

        public override string XmlNodeName
        {
            get
            {
                if (this.IsManifest)
                    return "FeatureManifest";
                else
                    return "TemplateFile";
            }
        }

        public override void AddToManifest(System.Xml.XmlDocument doc)
        {
            if (this.IsManifest) base.AddToManifest(doc);
        }
    }

}
